package com.dhl.fin.api.service;

import com.dhl.fin.api.common.dto.QueryDto;
import com.dhl.fin.api.common.service.CommonService;
import com.dhl.fin.api.common.util.CollectorUtil;
import com.dhl.fin.api.common.util.DateUtil;
import com.dhl.fin.api.common.util.ObjectUtil;
import com.dhl.fin.api.common.util.WebUtil;
import com.dhl.fin.api.dao.MyRemindDao;
import com.dhl.fin.api.dao.RemindTypeDao;
import com.dhl.fin.api.domain.RemindType;
import com.dhl.fin.api.enums.RemindTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

/**
 * @author CuiJianbo
 * @since 2025/5/4
 */
@Slf4j
@Service
@Transactional(rollbackFor = Exception.class)
public class RemindTypeService extends CommonService<RemindType> {

    @Autowired
    private MyRemindDao myRemindDao;

    public RemindType queryTypeByName(String name, String unicode) throws Exception {

        int n = count(QueryDto.builder()
                .available()
                .addWhere("unicode = '" + unicode + "'")
                .build());

        if (n == 0) {
            List<RemindType> remindTypes = initRemindType(unicode);
            return remindTypes.stream().filter(p -> p.getRemindTypeName().equals(name)).findFirst().orElse(null);
        } else {
            return get(QueryDto.builder()
                    .available()
                    .addWhere("remind_type_name = '" + name + "'")
                    .addWhere("unicode = '" + unicode + "'")
                    .build());
        }
    }

    public void checkHasRemindType(String unicode) throws Exception {
        RemindType remindType = get(QueryDto.builder()
                .available()
                .addWhere("unicode = '" + unicode + "'")
                .build());
        if (ObjectUtil.isNull(remindType)) {
            initRemindType(unicode);
        }
    }

    public List<RemindType> initRemindType(String unicode) throws Exception {
        List<RemindType> remindTypes = new LinkedList<>();
        for (RemindTypeEnum type : RemindTypeEnum.values()) {
            RemindType remindType1 = new RemindType();
            remindType1.setCreateTime(DateUtil.getSysTime());
            remindType1.setCreateUser("system");
            remindType1.setUpdateTime(DateUtil.getSysTime());
            remindType1.setUpdateUser("system");
            remindType1.setRemindTypeName(type.getName());
            remindType1.setUnicode(unicode);
            remindType1.setIsActive(true);
            remindType1.setIsFix(true);
            remindType1.setIcon(type.getCode());
            remindType1.setSort(type.getSort());
            remindTypes.add(remindType1);
        }
        myRemindDao.batchInsertRemindType(remindTypes);
        return remindTypes;
    }

    public void changePosition(Long id, Integer sort, String position) throws Exception {
        String unicode = WebUtil.getLoginUser().getUuid();

        if (ObjectUtil.isNull(id)) {
            initRemindType(unicode);
        }

        RemindType remindType = get(QueryDto.builder()
                .available()
                .addWhereWhen("sort < '" + sort + "'", position.equals("up"))
                .addWhereWhen("sort > '" + sort + "'", position.equals("down"))
                .addOrderWhen("sort desc", position.equals("up"))
                .addOrderWhen("sort asc", position.equals("down"))
                .addWhere("unicode = '" + unicode + "'")
                .addWhere("is_active = true")
                .build());

        if (ObjectUtil.notNull(remindType)) {
            updateFieldById(remindType.getId(), "sort", sort);
            updateFieldById(id, "sort", remindType.getSort());
        }

    }

    public List<RemindType> getAllRemindTypes(String unicode) throws Exception {
        return select(QueryDto.builder().available()
                .addWhere("unicode='" + unicode + "'")
                .addOrder("is_active desc, sort asc")
                .build());
    }

    public List<RemindType> getInitHomeRemindTypes(String unicode) throws Exception {
        List<RemindType> remindTypes = getAllRemindTypes(unicode);
        if (CollectorUtil.isEmpty(remindTypes)) {
            initRemindType(unicode);
        }

        return select(QueryDto.builder()
                .available()
                .addJoinDomain("remindTasks")
                .addJoinDomain("mensesRemindTasks")
                .addWhere("remind_type.unicode = '" + unicode + "'")
                .addWhere("remind_type.is_active = true")
                .addOrder("sort")
                .build());

    }

    @Override
    public String validate(RemindType domain) throws Exception {
        String unicode = WebUtil.getLoginUser().getUuid();
        int n = count(QueryDto.builder()
                .available()
                .addWhereWhen("id != " + domain.getId(), ObjectUtil.notNull(domain.getId()))
                .addWhere("unicode = '" + unicode + "'")
                .build());
        if (n > 0) {
            return "已经存在此类型";
        }
        return null;
    }
}
